package com.xd.community.controller.malls;

import com.xd.community.model.entity.Fans;
import com.xd.community.service.Iservice.IFansService;
import com.xd.common.response.BaseResponse;
import com.xd.common.response.CodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.json.JSONException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @Author: 林顺
 * @Date: 2021/2/20
 */
@RestController
@Api
@RequestMapping("/fans")
class FansInfoController {

    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(FansInfoController.class);

    @Resource
    private IFansService fansService;

    @ApiOperation(value="会员信息列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public BaseResponse getFansList() {
        List list = fansService.getFansList();
        return new BaseResponse(CodeEnum.SUCCESS.getCode(),CodeEnum.SUCCESS.getDesc(),list);
    }

    @ApiOperation(value="查询会员信息")
    @RequestMapping(value = "/fansone", method = RequestMethod.GET)
    public Fans getFansByOpenid(@ApiParam(value = "会员openid" , required = true) String openid) {
        return fansService.getFansByopenid(openid);
    }

    /*
     *会员注册
     */
    @ApiOperation(value="会员注册")
    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public BaseResponse getIndexInfo(@ApiParam(value = "会员基础参数") @RequestParam(value = "fans") Fans fans) throws JSONException {
        //核验会员注册相关信息
        Optional<Fans> opt = Optional.ofNullable(fans);
        if(opt.isPresent()) return new BaseResponse(CodeEnum.FAILED.getCode(),CodeEnum.FAILED.getDesc());
        fans = opt.get();
        fans.setCreatedate(new Date());
        fans.setCreateuser(opt.get().getOpenid());
        if (fansService.registerFans(fans)) {
            return new BaseResponse(CodeEnum.SUCCESS.getCode(),CodeEnum.SUCCESS.getDesc());
        }
        return new BaseResponse(CodeEnum.SUCCESS.getCode(),CodeEnum.SUCCESS.getDesc());
    }

}